package com.zbkj.common.utils;

import org.springframework.stereotype.Component;

import java.util.StringTokenizer;

/**
 * 拼音首字母大写工具类
 */
@Component
public class PinyinUtils {

    /**
     * 将字符串中的每个英文单词转换为首字母大写形式
     * @param input 输入字符串
     * @return 首字母大写后的字符串
     */
    public static String toPinyinCase(String input) {
        if (input == null || input.trim().isEmpty()) {
            return input;
        }
        
        StringBuilder result = new StringBuilder();
        StringTokenizer tokenizer = new StringTokenizer(input, " ", true);
        
        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();
            if (token.equals(" ")) {
                result.append(token);
            } else {
                result.append(capitalizeWord(token));
            }
        }
        
        return result.toString();
    }
    
    /**
     * 将单个单词转换为首字母大写形式
     * @param word 输入单词
     * @return 首字母大写后的单词
     */
    public static String capitalizeWord(String word) {
        if (word == null || word.isEmpty()) {
            return word;
        }
        
        // 处理可能包含标点符号的情况
        char[] chars = word.toCharArray();
        boolean foundFirstLetter = false;
        
        for (int i = 0; i < chars.length; i++) {
            if (Character.isLetter(chars[i])) {
                if (!foundFirstLetter) {
                    chars[i] = Character.toUpperCase(chars[i]);
                    foundFirstLetter = true;
                } else {
                    chars[i] = Character.toLowerCase(chars[i]);
                }
            }
        }
        
        return new String(chars);
    }
    
    /**
     * 测试方法
     */
    public static void main(String[] args) {
        // 测试用例
        String[] testCases = {
            "hello world",
            "zhang san",
            "li si",
            "wang wu",
            "java programming",
            "a b c d e",
            "mr. john smith",
            "o'reilly",
            "macdonald",
            "iphone"
        };
        
        for (String test : testCases) {
            System.out.println(test + " -> " + toPinyinCase(test));
        }
    }
}